import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login'
import Main from '@/views/Main'

// 重写push|replace方法，解决路由重复跳转报错
let originPush = VueRouter.prototype.push
let originReplace = VueRouter.prototype.replace
VueRouter.prototype.push = function (location, resolve, reject) {
    if (resolve && reject) {
        originPush.call(this, location, resolve, reject)
    } else {
        originPush.call(this, location, () => { }, () => { })
    }
}
VueRouter.prototype.replace = function (location, resolve, reject) {
    if (resolve && reject) {
        originReplace.call(this, location, resolve, reject)
    } else {
        originReplace.call(this, location, () => { }, () => { })
    }
}
Vue.use(VueRouter)

const routes = [
  {
    path: "*",
    redirect: "/404"
  },
  {
    path: '/404',
    component: () => import('@/views/404')
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/Register')
  },
  {
    path: '',
    component: Main,
    redirect: '/message',
    children: [
      {
        path: '/message',
        name: 'message',
        component: () => import('@/views/Main/Message'),
        meta: {
          isLogin: true
        },
      },
      {
        path: '/contacts',
        name: 'contacts',
        component: () => import('@/views/Main/Contacts'),
        meta: {
          isLogin: true
        },
      },
      {
        path: '/space',
        name: 'space',
        component: () => import('@/views/Main/Space'),
        meta: {
          isLogin: true
        },
      }
    ]
  },
  {
    path: '/messagedetails/:type',
    name: 'messagedetails',
    component: () => import('@/views/MessageDetails'),
    meta: {
      isLogin: true
    }
  },
  {
    path: '/userhome',
    name: 'userhome',
    component: () => import('@/views/UserHome'),
    meta: {
      isLogin: true
    }
  },
  {
    path: '/grouphome',
    name: 'grouphome',
    component: () => import('@/views/GroupHome'),
    meta: {
      isLogin: true
    }
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('@/views/Search'),
    meta: {
      isLogin: true
    }
  },
  {
    path: '/updateinfo/:type',
    name: 'updateinfo',
    component: () => import('@/views/UpdateInfo'),
    meta: {
      isLogin: true
    }
  },
  {
    path: '/creategroup/:type',
    name: 'creategroup',
    component: () => import('@/views/CreateOrUpdateGroup'),
    meta: {
      isLogin: true
    }
  },
  {
    path: '/spacedetails',
    name: 'spacedetails',
    component: () => import('@/views/SpaceDetails'),
    meta: {
      isLogin: true
    }
  },
  {
    path: '/agreeapply/:type',
    name: 'agreeapply',
    component: () => import('@/views/AgreeApply'),
    meta: {
      isLogin: true
    }
  }
]

const router = new VueRouter({
  routes,
  scrollBehavior () {
    return { x: 0, y: 0 }
  }
})

export default router
